package com.jay.beiange.efccp;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2016/12/23.
 */

public class WelComeActivity extends Activity{
    private ViewPager mViewPager;
    private List<ImageView> mImgList;
    private int[] mImgIds;

    private List<ImageView> mDotsList;
    private LinearLayout mDotLl;

    private Button mBtn;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_welcome);
        initView();
        initDate();
        initVp();
    }

    private void initView() {
        mViewPager = (ViewPager) findViewById(R.id.id_welcome_viewpager);
        mDotLl = (LinearLayout) findViewById(R.id.id_welcome_ll_dot);
        mBtn = (Button) findViewById(R.id.id_welcome_ll_btn);

    }

    private void initVp() {
        mViewPager.setAdapter(new PagerAdapter() {
            @Override
            public int getCount() {
                return mImgList.size();
            }

            @Override
            public boolean isViewFromObject(View view, Object object) {
                return view == object;
            }

            @Override
            public Object instantiateItem(ViewGroup container, int position) {
                container.addView(mImgList.get(position));
                return  mImgList.get(position);
            }
        });
        mViewPager.setCurrentItem(0);
        mViewPager.setOnPageChangeListener(new OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                for (int i=0;i<mImgIds.length;i++){
                    mDotsList.get(i).setBackgroundResource(R.mipmap.point_normal);
                }
                mDotsList.get(position).setBackgroundResource(R.mipmap.point_select);
                if (position == mImgList.size()-1) {
                    mBtn.setVisibility(View.VISIBLE);
                }else{
                    mBtn.setVisibility(View.GONE);
                }
                mBtn.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {


                        Intent intent = new Intent(WelComeActivity.this,MainActivity.class);
                        WelComeActivity.this.startActivity(intent);
                        WelComeActivity.this.finish();


                    }
                });
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }

    private void initDate() {

        mImgIds = new int[]{R.mipmap.welcome_guide_1,R.mipmap.welcome_guide_2};
        ImageView iv;
        mImgList = new ArrayList<ImageView>();
        for(int i=0;i<mImgIds.length;i++){
            iv = new ImageView(this);
            iv.setBackgroundResource(mImgIds[i]);
            mImgList.add(iv);
        }
        //添加点
        mDotsList = new ArrayList<ImageView>();

        for (int i=0;i<mImgIds.length;i++){
            ImageView dotView = new ImageView(this);
            dotView.setLayoutParams(new LinearLayout.LayoutParams(30,30));
            dotView.setBackgroundResource(R.mipmap.point_normal);
            mDotsList.add(dotView);
            mDotLl.addView(dotView);

        }
        mDotsList.get(0).setBackgroundResource(R.mipmap.point_select);
    }
}
